package xyz.redtorch.client.service.rpc.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xyz.redtorch.client.service.ClientTradeCacheService;
import xyz.redtorch.client.service.rpc.RpcClientRtnHandlerService;
import xyz.redtorch.common.service.FastEventService;
import xyz.redtorch.pb.CoreField.AccountField;
import xyz.redtorch.pb.CoreField.ContractField;
import xyz.redtorch.pb.CoreField.NoticeField;
import xyz.redtorch.pb.CoreField.OrderField;
import xyz.redtorch.pb.CoreField.PositionField;
import xyz.redtorch.pb.CoreField.TickField;
import xyz.redtorch.pb.CoreField.TradeField;
import xyz.redtorch.pb.CoreRpc.RpcAccountListRtn;
import xyz.redtorch.pb.CoreRpc.RpcAccountRtn;
import xyz.redtorch.pb.CoreRpc.RpcContractListRtn;
import xyz.redtorch.pb.CoreRpc.RpcContractRtn;
import xyz.redtorch.pb.CoreRpc.RpcNoticeRtn;
import xyz.redtorch.pb.CoreRpc.RpcOrderListRtn;
import xyz.redtorch.pb.CoreRpc.RpcOrderRtn;
import xyz.redtorch.pb.CoreRpc.RpcPositionListRtn;
import xyz.redtorch.pb.CoreRpc.RpcPositionRtn;
import xyz.redtorch.pb.CoreRpc.RpcTickListRtn;
import xyz.redtorch.pb.CoreRpc.RpcTickRtn;
import xyz.redtorch.pb.CoreRpc.RpcTradeListRtn;
import xyz.redtorch.pb.CoreRpc.RpcTradeRtn;

@Service
public class RpcClientRtnHandlerServiceImpl implements RpcClientRtnHandlerService {

	@Autowired
	private ClientTradeCacheService clientTradeCacheService;

	@Autowired
	private FastEventService fastEventService;

	@Override
	public void onOrderRtn(RpcOrderRtn rpcOrderRtn) {
		OrderField order = rpcOrderRtn.getOrder();
		fastEventService.emitOrder(order);
		clientTradeCacheService.storeOrder(order);
	}

	@Override
	public void onTradeRtn(RpcTradeRtn rpcTradeRtn) {
		TradeField trade = rpcTradeRtn.getTrade();
		fastEventService.emitTrade(trade);
		clientTradeCacheService.storeTrade(trade);
	}

	@Override
	public void onContractRtn(RpcContractRtn rpcContractRtn) {
		ContractField contract = rpcContractRtn.getContract();
		fastEventService.emitContract(contract);
		clientTradeCacheService.storeContract(contract);
	}

	@Override
	public void onPositionRtn(RpcPositionRtn rpcPositionRtn) {
		PositionField position = rpcPositionRtn.getPosition();
		fastEventService.emitPosition(position);
		clientTradeCacheService.storePosition(position);
	}

	@Override
	public void onAccountRtn(RpcAccountRtn rpcAccountRtn) {
		AccountField account = rpcAccountRtn.getAccount();
		fastEventService.emitAccount(account);
		clientTradeCacheService.storeAccount(account);
	}

	@Override
	public void onTickRtn(RpcTickRtn rpcTickRtn) {
		TickField tick = rpcTickRtn.getTick();
		fastEventService.emitTick(tick);
		clientTradeCacheService.storeTick(tick);
	}

	@Override
	public void onNoticeRtn(RpcNoticeRtn rpcNoticeRtn) {
		NoticeField notice = rpcNoticeRtn.getNotice();
		fastEventService.emitNotice(notice);
	}

	@Override
	public void onOrderListRtn(RpcOrderListRtn rpcOrderListRtn) {
		for (OrderField order : rpcOrderListRtn.getOrderList()) {
			fastEventService.emitOrder(order);
			clientTradeCacheService.storeOrder(order);
		}
	}

	@Override
	public void onTradeListRtn(RpcTradeListRtn rpcTradeListRtn) {
		for (TradeField trade : rpcTradeListRtn.getTradeList()) {
			fastEventService.emitTrade(trade);
			clientTradeCacheService.storeTrade(trade);
		}
	}

	@Override
	public void onContractListRtn(RpcContractListRtn rpcContractListRtn) {
		for (ContractField contract : rpcContractListRtn.getContractList()) {
			fastEventService.emitContract(contract);
			clientTradeCacheService.storeContract(contract);
		}
	}

	@Override
	public void onPositionListRtn(RpcPositionListRtn rpcPositionListRtn) {
		for (PositionField position : rpcPositionListRtn.getPositionList()) {
			fastEventService.emitPosition(position);
			clientTradeCacheService.storePosition(position);
		}
	}

	@Override
	public void onAccountListRtn(RpcAccountListRtn rpcAccountListRtn) {
		for (AccountField account : rpcAccountListRtn.getAccountList()) {
			fastEventService.emitAccount(account);
			clientTradeCacheService.storeAccount(account);
		}

	}

	@Override
	public void onTickListRtn(RpcTickListRtn rpcTickListRtn) {
		for (TickField tick : rpcTickListRtn.getTickList()) {
			fastEventService.emitTick(tick);
			clientTradeCacheService.storeTick(tick);
		}
	}

}
